#include "staff.h"

//管理员查询
void do_admin_query(int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	int a = 0;
	char buf[1024];
	//char name[32];
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	printf("*************************************************************\n");
	printf("**********************1:查找所有  2:退出**********************\n");
	printf("请输入选择的数字>>");
	scanf("%d",&a);
	switch(a){
	case 1:
		msg->msgtype = ADMIN_QUERY;
		msg->usertype = ADMIN;
		msg->flags = FINDALL;
		send(fd,msg,sizeof(Msg),0);
		bzero(buf,sizeof(buf));
		recv(fd,msg,sizeof(Msg),0);
		printf("%-17s\n",msg->recvmsg);
		break;
	case 2:
		msg->msgtype = QUIT;
		send(fd,msg,sizeof(Msg),0);
		close(fd);
		break;
	default:
		printf("输入错误，请重新输入");
		break;
	}
}

//管理员修改
void do_admin_modification(int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	int id = 0;
	double salary = 0;
	int a = 0;
	char buf[DATALEN] = "";
	printf("请输入你要修改的员工的工号>>");
	scanf("%d",&id);
	msg->info.id = id;
	char *buf_msg[9] = {"姓名","年龄","家庭住址","电话","职位","工资","入职年月","评级","密码"};
	puts("*******************请输入要修改的选项********************");
	puts("******	1：姓名  2：年龄  3：家庭住址  4：电话  *******");
	puts("******	5：职位	 6：工资  7：入职年月  8：评级  *******");
	puts("******	9：密码	 10：退出				 	    *******");
	printf("请输入要修改的选项>>");
	scanf("%d",&id);
	if(id == 10){
		exit(0);
	}
	printf("请输入新的%s>>",buf_msg[id-1]);
	switch(id){
	case 1:
		bzero(buf,sizeof(buf));
		scanf("%s",buf);
		msg->flags = NAME;
		strcpy(msg->info.name,buf);
		break;
	case 2:
		scanf("%d",&a);
		msg->info.age = a;
		msg->flags = AGE;
		break;
	case 3:
		bzero(buf,sizeof(buf));
		scanf("%s",msg->info.address);
		msg->flags = ADDR;
		break;
	case 4:
		bzero(buf,sizeof(buf));
		scanf("%s",msg->info.telphone);
		msg->flags = PHONE;
		break;
	case 5:
		bzero(buf,sizeof(buf));
		scanf("%s",buf);
		msg->flags = WORK;
		strcpy(msg->info.position,buf);
		break;
	case 6:
		scanf("%lg",&salary);
		msg->info.salary = salary;
		msg->flags = SALARY;
		break;
	case 7:
		bzero(buf,sizeof(buf));
		scanf("%s",buf);
		msg->flags = DATE;
		strcpy(msg->info.data_entry,buf);
		break;
	case 8:
		scanf("%d",&a);
		msg->info.rate = a;
		msg->flags = LEVEL;
		break;
	case 9:
		bzero(buf,sizeof(buf));
		scanf("%s",buf);
		msg->flags = PASSWORD;
		strcpy(msg->info.passwd,buf);
		break;
	default:
		printf("%s:%s:%d\n",__FILE__,__func__,__LINE__);
	}
	msg->msgtype = ADMIN_MODIFY;
	send(fd,msg,sizeof(Msg),0);
	recv(fd,msg,sizeof(Msg),0);
	printf("%s\n",msg->recvmsg);
}




// 管理员添加用户
void do_admin_adduser(int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);	
	char yn;
	while(1){
		printf("请输入工号>>");
		scanf("%d",&msg->info.id);
		printf("您输入的工号是%d\n",msg->info.id);
		puts("请输入用户名>>");
		getchar();
		scanf("%s",msg->info.name);
		puts("请输入用户密码>>");
		getchar();
		scanf("%s",msg->info.passwd);
		puts("请输入年龄>>");
		getchar();
		scanf("%d",&msg->info.age);
		puts("请输入电话>>");
		getchar();
		scanf("%s",msg->info.telphone);
		puts("请输入家庭住址>>");
		getchar();
		scanf("%s",msg->info.address);
		puts("请输入职位>>");
		getchar();
		scanf("%s",msg->info.position);
		puts("请输入入职日期(格式:----.--.--)>>");
		getchar();
		scanf("%s",msg->info.data_entry);
		puts("请输入评级>>");
		getchar();
		scanf("%d",&msg->info.rate);
		puts("请输入工资>>");
		getchar();
		scanf("%lg",&msg->info.salary);
		puts("是否为管理员(y/n)>>");
		getchar();
		scanf("%c",&yn);
		if(yn == 'y'){
			msg->info.usertype = ADMIN;
		}else if(yn == 'n'){
			msg->info.usertype = USER;
		}else{
			puts("输入错误,此用户为普通用户，如需管理员权限，请完善信息后更改");
			msg->info.usertype  = USER;
		}
		bzero(msg->recvmsg,4096);
		msg->msgtype = ADMIN_ADDUSER;
		send(fd,msg,sizeof(Msg),0);
		recv(fd,msg,sizeof(Msg),0);
		printf("%s\n",msg->recvmsg);
		printf("是否继续添加员工(y/n)>>");
		getchar();
		scanf("%c",&yn);
		if(yn == 'n'){
			break;
		}else if(yn == 'y'){
			continue;
		}else{
			break;
		}
	}
}

//管理员删除用户
void do_admin_deluser(int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	int a = 0;
	printf("请输入要删除的员工工号>>");
	scanf("%d",&a);
	printf("请输入要删除的员工姓名>>");
	scanf("%s",msg->info.name);
	msg->info.id = a;
	msg->msgtype = ADMIN_DELUSER;
	send(fd,msg,sizeof(Msg),0);
	recv(fd,msg,sizeof(Msg),0);
	printf("%s\n",msg->recvmsg);
}

//历史记录查询
void do_admin_history (int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	msg->msgtype = ADMIN_HISTORY;
	send(fd,msg,sizeof(Msg),0);
	recv(fd,msg,sizeof(Msg),0);
	printf("%s\n",msg->recvmsg);
}

//删除历史记录
void do_admin_delhistory(int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	msg->msgtype = ADMIN_DELHISTORY;
	send(fd,msg,sizeof(Msg),0);
	recv(fd,msg,sizeof(Msg),0);
	printf("%s\n",msg->recvmsg);
}

//管理员的菜单
void admin_menu(int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	int flag;
	while(1)
	{
		printf("*************************************************************\n");
		printf("* 1：查询  2：修改 3：添加用户  4：删除用户  5：查询历史记录*\n");
		printf("* 6：删除历史记录  7：退出									*\n");
		printf("*************************************************************\n");
		printf("请输入您要操作的功能:");
		scanf("%d",&flag);
		getchar();
		switch(flag)
		{
		case 1:
			do_admin_query(fd,msg);
			break;
		case 2:
			do_admin_modification(fd,msg);
			break;
		case 3:
			do_admin_adduser(fd,msg);
			break;
		case 4:
			do_admin_deluser(fd,msg);
			break;
		case 5:
			do_admin_history(fd,msg);
			break;
		case 6:
			do_admin_delhistory(fd,msg);
			break;
		case 7:
			msg->msgtype = QUIT;
			send(fd, msg, sizeof(Msg), 0);
			close(fd);
			exit(1);
		default:
			printf("您输入有误，请重新输入！\n");
		}
	}
}

//用户查询
void do_user_query(int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	printf("请输入您的工号>>");
	scanf("%d",&msg->info.id);
	msg->msgtype = USER_QUERY;
	send(fd,msg,sizeof(Msg),0);
	char buf[1024];
	bzero(buf,sizeof(buf));
	recv(fd,msg,sizeof(Msg),0);
	puts("您的信息:");
	printf("%-7s\n",msg->recvmsg);

}

//用户修改
void do_user_modification(int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	printf("请输入您的工号");
	int a = 0;
	scanf("%d",&msg->info.id);
	printf("***********请输入要修改的选项(其他信息亲请联系管理员)*********");
	printf("***********	1：家庭住址   2：电话  3：密码  4：退出***********");
	printf("**************************************************************");
	scanf("%d",&a);
	char *buf_msg[4] = {"家庭住址","电话","密码"};
	printf("请输入新的%s>>",buf_msg[a-1]);
	char buf[DATALEN] = "";
	switch(a){
	case 1:
		bzero(buf,sizeof(buf));
		scanf("%s",buf);
		strcpy(msg->info.address,buf);
		msg->flags = ADDR;
		break;
	case 2:
		bzero(buf,sizeof(buf));
		scanf("%s",buf);
		strcpy(msg->info.telphone,buf);
	msg->flags = PHONE;
		break;
	case 3:
		bzero(buf,sizeof(buf));
		scanf("%s",buf);
		strcpy(msg->info.passwd,buf);
		msg->flags = PASSWORD;
		break;
	case 4:
		break;
	default:
		puts("输入错误");
		break;
	}
	msg->msgtype = USER_MODIFY;
	send(fd,msg,sizeof(Msg),0);
	recv(fd,msg,sizeof(Msg),0);
	printf("%s\n",msg->recvmsg);
}



//用户菜单
void user_menu(int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	int n;
	while(1)
	{
		printf("*************************************************************\n");
		printf("*************  1：查询  	2：修改		3：退出	 *************\n");
		printf("*************************************************************\n");
		printf("请输入您的选择（数字）>>");
		scanf("%d",&n);
		getchar();
		switch(n)
		{
		case 1:
			do_user_query(fd,msg);
			break;
		case 2:
			do_user_modification(fd,msg);
			break;
		case 3:
			msg->msgtype = QUIT;
			send(fd, msg, sizeof(Msg), 0);
			close(fd);
			exit(1);
		default:
			printf("您输入有误，请输入数字\n");
			break;
		}
	}
}



//登录
int admin_or_user_login(int fd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	bzero(msg->username,DATALEN);
	printf("请输入用户名:");
	scanf("%s",msg->username);
	getchar();
	bzero(msg->passwd,6);
	printf("请输入密码:");
	scanf("%s",msg->passwd);
	getchar();
	send(fd,msg,sizeof(Msg),0);
	recv(fd,msg,sizeof(Msg),0);
	printf("msg->recvmsg:%s\n",msg->recvmsg);
	if(strncmp(msg->recvmsg,"OK",2) == 0){
		if(msg->usertype == ADMIN)
		{
			printf("进入管理员系统\n");
			admin_menu(fd,msg);
		}
		else if(msg->usertype == USER)
		{
			printf("进入用户系统\n");
			user_menu(fd,msg);
		}
	}
	else
	{
		printf("登陆失败！%s\n", msg->recvmsg);
		return -1;
	}
	return SUCCESS;
}


int log_in(int fd)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	int flag;
	Msg msg;
	while(1){
		info_print();
		scanf("%d",&flag);
		getchar();
		switch(flag){
		case 1:
			msg.msgtype = ADMIN_LOGIN;
			msg.usertype = ADMIN;
			break;
		case 2:
			msg.msgtype = USER_LOGIN;
			msg.usertype = USER;
			break;
		case 3:
			msg.msgtype = QUIT;
			if(send(fd,&msg,sizeof(Msg),0)<0)
			{
				perror("do_login send");
				return -1;
			}
			close(fd);
			exit(0);
			break;
		default:
			printf("输入错误，重新登录\n");
		}
		admin_or_user_login(fd,&msg);
	}
	return SUCCESS;
}









